#ifndef EXECUTOR_H
#define EXECUTOR_H

#include <vector>
#include <cstdint>
#include <string>
#include <memory>

#include "MyShellNamespace.h"
#include "MyShellParser.h"
#include "MyShellActionScope.h"

MY_SHELL_NAMESPACE_BEGIN
class Executor
{
public:
    // 执行内部命令
    static void executeInternalCommands(const std::uint32_t& internal_command_id, const std::vector<std::string>& splited_command);
    // 执行外部命令
    static void executeExternalCommands(std::vector<std::string>& splited_command);
    // 执行系统命令
    static void executeSystemCommands(std::vector<std::string>& splited_command);
    // 执行管道命令
    static void executePipe(std::vector<std::string>& splited_command);
    // 执行别名、变量赋值命令，与内部命令alias有区别
    static void executeAlias(std::vector<std::string>& splited_command);
    // 执行一行命令
    static void executeLineCommand(const std::string& command);
};

class MyShellScriptExecutor
{
private:
    // 子执行流
    // 在一个作用域上执行一系列语句
    static void executeSeriesStatements(mysh::MyShellActionScope& action_scope, const std::vector<std::shared_ptr<mysh::GrammarNode>>& grammar_nodes);
    // 执行声明语句
    static std::vector<std::string> executeDeclareStatement(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node);
    // 执行运行语句
    static void executeRunStatement(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node);
    // 执行输入语句
    static void executeInputStatement(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node);
    // 执行输出语句
    static void executeOutputStatement(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node);
    // 执行赋值语句
    static void executeAssignStatement(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node);
    // 执行if-else语句块
    static void executeIfElseStatementBlock(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node);
    // 执行for-in语句块
    static void executeForInStatementBlock(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node);
    // 执行for-to语句块
    static void executeForToStatementBlock(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node);
private:
    // 私有辅助函数
    // 解析数值节点，将数值存储到对应的寄存器
    static void parseDataNode(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::DataNode>& data_node);
    // 解析左值，获取索引数组
    static std::vector<std::int32_t> parseLeftValueNode(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::LeftValueNode>& left_value_node);
public:
    // 执行脚本
    static void executeScript(const std::vector<std::shared_ptr<mysh::GrammarNode>>& grammar_nodes);
};
MY_SHELL_NAMESPACE_END

#endif // ! EXECUTOR_H